{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import * from utils\n",
    "numbins = 15\n",
    "bins = np.linspace(0,1, numbins+1)\n",
    "sigma = 1500\n",
    "GoGaussian = False\n",
    "nF2 = 3\n",
    "num_bins = 15\n",
    "\n",
    "for rat_name, mod_name, sess_name, day_name in (('R', '1', 'OF', 'day1'),\n",
    "                                                ('R', '2', 'OF', 'day1'),\n",
    "                                                ('R', '3', 'OF', 'day1'),\n",
    "                                                ('R', '1', 'WW', 'day1'),\n",
    "                                                ('R', '2', 'WW', 'day1'),\n",
    "                                                ('R', '3', 'WW', 'day1'),\n",
    "                                                ('R', '1', 'OF', 'day2'),\n",
    "                                                ('R', '2', 'OF', 'day2'),\n",
    "                                                ('R', '3', 'OF', 'day2'),\n",
    "                                                ('Q', '1', 'OF', ''),\n",
    "                                                ('Q', '2', 'OF', ''),\n",
    "                                                ('Q', '1', 'WW', ''),\n",
    "                                                ('Q', '2', 'WW', ''),\n",
    "                                                ('S', '1', 'OF', ''),\n",
    "                                                ('S', '1', 'WW', ''),\n",
    "                                                ):\n",
    "    \n",
    "    file_name =  rat_name + '_' + mod_name + '_' + sess_name\n",
    "    if len(day_name)>0:\n",
    "        file_name += '_' + day_name  \n",
    "        \n",
    "    spikes, xx, yy,__,__ = get_spikes(rat_name, mod_name, day_name, sess_name, bType = 'pure',\n",
    "                                     bSmooth = False, bSpeed = True)\n",
    "    T, num_neurons = np.shape( spikes)\n",
    "    \n",
    "    ypt_all = []\n",
    "    Lvals_tor = []\n",
    "    P_tor_all = np.zeros((num_neurons, num_bins**2, nF2) )\n",
    "    LOOtorscores = np.zeros((num_neurons))\n",
    "\n",
    "    filenames = glob.glob('Toroidal_topology_grid_cell_data/Results/' + file_name + '_coords*')\n",
    "    coordsall = {}\n",
    "    if len(filenames) == 1:\n",
    "        f = np.load(filenames[0], allow_pickle = True) \n",
    "        coordsall = f['coordsall'][()]\n",
    "        times = f['times']\n",
    "        f.close()\n",
    "    else:\n",
    "        continue\n",
    "    for LAM in (np.sqrt(1),np.sqrt(10),np.sqrt(100),np.sqrt(1000)):\n",
    "        for n in np.arange(0, num_neurons, 1): \n",
    "            coords2 = coordsall[n].copy()\n",
    "            ypt_all.append([])\n",
    "            Lvals_tor.append([])\n",
    "            LOOtorscores[n] = glm2d(coords2[times,:], spikes[times,n], \n",
    "                                    num_bins, True, LAM, GoGaussian, nF2)\n",
    "        if GoGaussian:\n",
    "            np.savez_compressed('Toroidal_topology_grid_cell_data/Results/' + file_name + '_1_G_sd' + str(int(LAM)), \n",
    "                LOOtorscores = LOOtorscores)            \n",
    "        else:\n",
    "            np.savez_compressed('Toroidal_topology_grid_cell_data/Results/' + file_name + '_1_P_sd' + str(int(LAM)), \n",
    "                LOOtorscores = LOOtorscores)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import wilcoxon\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "minEV = np.inf\n",
    "maxEV = -np.inf\n",
    "\n",
    "Explained_deviance_Toroidal_OF = []\n",
    "Explained_deviance_Spatial_OF = []\n",
    "\n",
    "for rat_name, mod_name, sess_name, day_name in (('R', '1', 'OF', 'day1'),\n",
    "                                                ('R', '2', 'OF', 'day1'),\n",
    "                                                ('R', '3', 'OF', 'day1'),\n",
    "                                                ('R', '1', 'OF', 'day2'),\n",
    "                                                ('R', '2', 'OF', 'day2'),\n",
    "                                                ('R', '3', 'OF', 'day2'),\n",
    "                                                ('Q', '1', 'OF', ''),\n",
    "                                                ('Q', '2', 'OF', ''),\n",
    "                                                ('S', '1', 'OF', ''),\n",
    "                                               ):            \n",
    "    file_name = rat_name + '_' + mod_name + '_' + sess_name\n",
    "    torscores = []\n",
    "    spacescores = []\n",
    "    torscoresmean = []\n",
    "    spacescoresmean = []\n",
    "    for LAM in (np.sqrt(1),np.sqrt(10),np.sqrt(100),np.sqrt(1000)):\n",
    "        f = np.load('Toroidal_topology_grid_cell_data/Results/' + file_name + '_info.npz', allow_pickle = True)\n",
    "        torscores.append(f['I_torus'])\n",
    "        spacescores.append(f['I_space'])\n",
    "        torscoresmean.append(np.mean(f['I_torus']))\n",
    "        spacescores.append(np.mean(f['I_space']))\n",
    "        f.close()\n",
    "\n",
    "    torscores = torscores[np.argmax(torscoresmean)]\n",
    "    spacescores = spacescores[np.argmax(spacescoresmean)]\n",
    "    maxEV =  max(maxEV, max(np.max(torscores), np.max(spacescores)))\n",
    "    minEV =  min(minEV, min(np.min(torscores), np.min(spacescores)))\n",
    "    ax.scatter(spacescores, torscores, s = 10, c = colors_envs[file_name])\n",
    "    p  = wilcoxon(torscores-spacescores, alternative='greater')\n",
    "    print(file_name)\n",
    "    print(' n: ' + str(len(torscores)) )\n",
    "    print(' p: ' + str(p) )\n",
    "    print('')\n",
    "\n",
    "    Explained_deviance_Toroidal_OF.extend([np.mean(torscores)])\n",
    "    Explained_deviance_Toroidal_OF.extend([np.std(torscores)/np.sqrt(len(torscores))])\n",
    "    Explained_deviance_Spatial_OF.extend([np.mean(spacescores)])\n",
    "    Explained_deviance_Spatial_OF.extend([np.std(spacescores)/np.sqrt(len(spacescores))])\n",
    "ax.plot([minEV, maxEV], [minEV, maxEV], c='k', zorder = -5)\n",
    "ax.set_xlim([minEV-(maxEV-minEV)*0.05,maxEV+(maxEV-minEV)*0.05])\n",
    "ax.set_ylim([minEV-(maxEV-minEV)*0.05,maxEV+(maxEV-minEV)*0.05])\n",
    "ax.set_aspect('equal', 'box')\n",
    "ax.xaxis.set_tick_params(width=1, length =5)\n",
    "ax.yaxis.set_tick_params(width=1, length =5)\n",
    "\n",
    "plot_stats(Explained_deviance_Toroidal_OF, Explained_deviance_Spatial_OF,  \n",
    "    Explained_deviance_OF_lbls, 'info', 'OF')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import wilcoxon\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "minEV = np.inf\n",
    "maxEV = -np.inf\n",
    "\n",
    "Explained_deviance_ToroidalC = []\n",
    "Explained_deviance_SpatialC = []\n",
    "\n",
    "for rat_name, mod_name, sess_name, day_name in (('R', '1', 'WW', 'day1'),\n",
    "                                                ('R', '2', 'WW', 'day1'),\n",
    "                                                ('R', '3', 'WW', 'day1'),\n",
    "                                                ('Q', '1', 'WW', ''),\n",
    "                                                ('Q', '2', 'WW', ''),\n",
    "                                                ('S', '1', 'WW', ''),\n",
    "                                               ):            \n",
    "            file_name = rat_name + '_' + mod_name + '_' + sess_name\n",
    "            torscores = []\n",
    "            spacescores = []\n",
    "            torscoresmean = []\n",
    "            spacescoresmean = []\n",
    "            for LAM in (np.sqrt(1),np.sqrt(10),np.sqrt(100),np.sqrt(1000)):\n",
    "                f = np.load('Toroidal_topology_grid_cell_data/Results/' + file_name + '_info.npz', allow_pickle = True)\n",
    "                torscores = f['I_torus']\n",
    "                spacescores = f['I_space']\n",
    "                torscoresmean.append(np.mean(f['I_torus']))\n",
    "                spacescores.append(np.mean(f['I_space']))\n",
    "                f.close()\n",
    "            torscores = torscores[np.argmax(torscoresmean)]\n",
    "            spacescores = spacescores[np.argmax(spacescoresmean)]\n",
    "\n",
    "            maxEV =  max(maxEV, max(np.max(torscores), np.max(spacescores)))\n",
    "            minEV =  min(minEV, min(np.min(torscores), np.min(spacescores)))\n",
    "            ax.scatter(spacescores, torscores, s = 10, c = colors_envs[file_name])\n",
    "            p  = wilcoxon(torscores-spacescores, alternative='greater')\n",
    "            print(file_name)\n",
    "            print(' n: ' + str(len(torscores)) )\n",
    "            print(' p: ' + str(p) )\n",
    "            print('')\n",
    "\n",
    "            Explained_deviance_ToroidalC.extend([np.mean(torscores)])\n",
    "            Explained_deviance_ToroidalC.extend([np.std(torscores)/np.sqrt(len(torscores))])\n",
    "            Explained_deviance_SpatialC.extend([np.mean(spacescores)])\n",
    "            Explained_deviance_SpatialC.extend([np.std(spacescores)/np.sqrt(len(spacescores))])\n",
    "ax.plot([minEV, maxEV], [minEV, maxEV], c='k', zorder = -5)\n",
    "ax.set_xlim([minEV-(maxEV-minEV)*0.05,maxEV+(maxEV-minEV)*0.05])\n",
    "ax.set_ylim([minEV-(maxEV-minEV)*0.05,maxEV+(maxEV-minEV)*0.05])\n",
    "ax.set_aspect('equal', 'box')\n",
    "ax.xaxis.set_tick_params(width=1, length =5)\n",
    "ax.yaxis.set_tick_params(width=1, length =5)\n",
    "\n",
    "plot_stats(Explained_deviance_ToroidalC, Explained_deviance_SpatialC,  \n",
    "    Explained_devianceC_lbls, 'ED', 'WW')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
